<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../inc.jsp"></jsp:include>
		<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript" charset="utf-8">
	var datagrid,lastIndex,clid = '${clid}';
	
	$(function() {

		datagrid = $('#dg').datagrid({
			url : 'goodsCheckItemAction!noAuth_datagrid.html?clid='+clid,
			iconCls : 'icon-save',
			pagination : true,
			pagePosition : 'bottom',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40 ],
			fit : true,
			fitColumns : false,
			nowrap : false,
			border : false,
			idField : 'id',
			sortName : 'id',
			sortOrder : 'desc',
			checkOnSelect : false,
			selectOnCheck : true,
			frozenColumns : [ [ {
				title : '编号',
				field : 'id',
				width : 150,
				sortable : true,
				hidden : true
			},  {
				title : '商品条码',
				field : 'barcode',
				width : 120
			},  {
				title : '款号',
				field : 'type',
				width : 80
			}] ],
			columns : [ [{
				title : '商品名称',
				field : 'gname',
				width : 80
			}, {
				title : '仓库ID',
				field : 'rid',
				width : 100,
				hidden : true
			}, {
				title : '仓库名称',
				field : 'rname',
				width : 80
			}, {
				title : '商品ID',
				field : 'gid',
				width : 100,
				hidden : true
			},  {
				title : '初始库存',
				field : 'initAmount',
				width : 55
			},  {
				title : '进货总数',
				field : 'orderTotal',
				width : 55
			},  {
				title : '进货退货总数',
				field : 'orderReturnTotal',
				width : 70
			},  {
				title : '销售总数',
				field : 'saleTotal',
				width : 55
			},  {
				title : '销售退货总数',
				field : 'saleReturnTotal',
				width : 70
			} ,  {
				title : '报损总数',
				field : 'damageTotal',
				width : 55
			}
			/* ,  {
				title : '调拔总数',
				field : 'transferTotal',
				width : 55
			}*/,  {
				title : '库存总数',
				field : 'repertoryTotal',
				width : 55
			},  {
				title : '盘前库存',
				field : 'beforeCheck',
				width : 55
			},  {
				title : '盘点库存',
				field : 'checkAmount',
				width : 55,
				editor:{type:'numberbox'}
			},  {
				title : '已盘点',
				field : 'isChecked',
				width : 45,
				formatter : function(value, row, index) {
					if(row.isChecked){
						return '1';
					}else{
						return '0';
					}
				}
			},  {
				title : '盘点时间',
				field : 'checkDate',
				width : 100,
				formatter : function(value, row, index) {
					if(value!=null){
				    	return value.substring(0,10);
					}
				}
			}]],
			toolbar : [ {
				text : '保存',
				iconCls : 'icon-add',
				handler : function() {
					append();
				}
			}, '-', {
				text : '重盘',
				iconCls : 'icon-remove',
				handler : function() {
					reset();
				}
			}, '-', {
				text : '取消选中',
				iconCls : 'icon-undo',
				handler : function() {
					datagrid.datagrid('clearSelections');
					datagrid.datagrid('unselectAll');
				}
			}/*, '-', {
				text : '导出盘点',
				iconCls : 'icon-undo',
				handler : function() {
					datagrid.datagrid('clearSelections');
					datagrid.datagrid('unselectAll');
				}
			}, '-', {
				text : '导入盘点',
				iconCls : 'icon-undo',
				handler : function() {
					datagrid.datagrid('clearSelections');
					datagrid.datagrid('unselectAll');
				}
			}*/, '-'],
			onClickRow:function(rowIndex){
				var rows = $('#dg').datagrid('getRows');
				var row = rows[rowIndex];
				row.checkDate = getDate();
				
				if(lastIndex != rowIndex){
					$('#dg').datagrid('endEdit', lastIndex);
					$('#dg').datagrid('beginEdit', rowIndex);
					lastIndex = rowIndex;
				}else{
					$('#dg').datagrid('endEdit', lastIndex);
					$('#dg').datagrid('beginEdit', rowIndex);
					lastIndex = rowIndex;
				}
			}
		});
		
		var irid = $('#searchForm').find('input[name=irid]');
			var iridCombobox = irid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});
			
			var orid = $('#searchForm').find('input[name=orid]');
			var oridCombobox = orid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});

	});
	
	function append() {
		var rows = $('#dg').datagrid('getRows');
		var row = rows[lastIndex];
		row.checkDate = getDate();
		
		$('#dg').datagrid('acceptChanges');
		var items = [],hasChecked = true;
		for ( var i = 0; i < rows.length; i++) {
			if(rows[i].checkDate){
				items.push(rows[i].id);
				items.push(rows[i].checkAmount);
				items.push(rows[i].checkDate);
			}else{
				hasChecked = false;
			}
		}
		$('#items').val(items.join(','));
		
		var msg = null;
		if(!hasChecked){
			msg = "还有没盘点的商品，确定保存?";
		}else{
			msg = "确定盘完库存并保存?";
		}
		parent.sy.messagerConfirm('请确认', msg, function(r) {
			if(r){
				$('#checkForm').form('submit', {
					url : '${pageContext.request.contextPath}/kcgl/goodsCheckAction!edit.html',
					success : function(d) {
						var json = $.parseJSON(d);
						parent.sy.messagerShow({
							msg : json.msg,
							title : '提示'
						});
 						if (json.success) {
 							if (parent.centerTabs.tabs('exists', '新增库存盘点单')) {
 								parent.showTab("新增库存盘点单","库存盘点");
 							}else if(parent.centerTabs.tabs('exists', '修改库存盘点单')){
 								parent.showTab("修改库存盘点单","库存盘点");
 							}else{
 								parent.showTab("","库存盘点");
 							}
 						}
					}
				});
			}
		});
	}
	
	function reset() {
		parent.sy.messagerConfirm('请确认', '重新盘点库存将会清除上次盘点的信息，您确认要重新盘点库存吗？', function(r) {
			if(r){
				$.ajax({
					url : '${pageContext.request.contextPath}/kcgl/goodsCheckAction!reset.html',
					data : {id:clid},
					dataType : 'json',
					success : function(d) {
						datagrid.datagrid('load');
						datagrid.datagrid('unselectAll');
						parent.sy.messagerShow({
							title : '提示',
							msg : d.msg
						});
					}
				});
			}
		});
	}
	
	function getDate(){
		var date = new Date();
		var year = date.getFullYear()
		var month = date.getMonth()+1;
		var day = date.getDate();
		return year+"-"+(month>9?month:("0"+month))+"-"+(day>9?day:("0"+day));
	}
</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">

		<div data-options="region:'north',border:false" style="height: 30px; overflow: hidden;">
			<form id="checkForm" style="height:30px;">
			<input type="hidden" name="id" value="${clid }">
			<input type="hidden" name="items" id="items"/>
			<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
				<tr>
					<th style="width: 70px;">当前仓库：</th>
					<td>${rname }</td>
				</tr>
			</table>
			</form>
		</div>

		<div data-options="region:'center',border:false" style="overflow: hidden;">
			<table id="dg"></table>
		</div>
	</body>
</html>